package com.sx.controller;

import com.sx.basic.ResponseEntity;
import com.sx.basic.pageable.Condition;
import com.sx.basic.pageable.PageRequest;
import com.sx.entity.user.UserEntity;
import com.sx.mapping.UserMapping;
import com.sx.service.user.IUserService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author AdinZ
 * @date 2020/11/19 11:17
 */
@RestController
public class UserController {

    @Resource private IUserService userService;

    @GetMapping(UserMapping.USER) @ApiOperation("用户列表")
    public ResponseEntity<List<UserEntity>> pageUser(@RequestParam("page")int page,
                                                     @RequestParam("size")int size,
                                                     @RequestParam(value = "phone",required = false)String phone,
                                                     @RequestParam(value = "nickname",required = false)String nickname,
                                                     @RequestParam(value = "username",required = false)String username){
        Condition condition=new Condition();
        if(!StringUtils.isEmpty(phone)) condition.eq("phone",phone);
        if(!StringUtils.isEmpty(nickname)) condition.eq("nickname",nickname);
        if(!StringUtils.isEmpty(username)) condition.eq("username",username);
        return this.userService.findPage(condition, PageRequest.of(page,size, Sort.Direction.DESC,"id"));
    }

    @PostMapping(UserMapping.USER_STATE)@ApiOperation("用户状态更新")
    public ResponseEntity userState(@RequestParam("id")Long[] ids,@RequestParam("state")int state){
        this.userService.userState(ids,state);
        return new ResponseEntity().toSuccess();
    }





}
